package com.mooc.house.controller;

import com.mooc.house.common.CommonConstants;
import com.mooc.house.common.PageParams;
import com.mooc.house.common.RestCode;
import com.mooc.house.common.RestResponse;
import com.mooc.house.model.*;
import com.mooc.house.service.CityService;
import com.mooc.house.service.CommunityService;
import com.mooc.house.service.HouseService;
import com.mooc.house.service.RecommendService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/house")
public class HouseController {

    @Autowired
    private CityService cityService;

    @Autowired
    private CommunityService communityService;

    @Autowired
    private HouseService houseService;

    @Autowired
    private RecommendService recommendService;

    @RequestMapping("/getAllCitys")
    public RestResponse<List<City>> getAllCitys() {
        List<City> cities = cityService.selectAllCities();
        if(CollectionUtils.isEmpty(cities)){
            return RestResponse.error(RestCode.CITY_NOT_FOUND);
        }
        return RestResponse.success(cities);

    }

    @RequestMapping("/getAllCommunity")
    public RestResponse<List<Community>> getAllCommunity(){

        List<Community> allCommunity = communityService.getAllCommunity();
        if(CollectionUtils.isEmpty(allCommunity)){
            return RestResponse.error(RestCode.COMMUNITY_NOT_FOUND);
        }
        return RestResponse.success(allCommunity);
    }

    @RequestMapping("/add")
    public RestResponse add(@RequestBody House house){

         if(house.getUserId() == null){
             return RestResponse.error(RestCode.ILLEGAL_PARAMS);
         }
         return houseService.add(house,house.getUserId());
    }

    @RequestMapping("/getHouses")
    public RestResponse<ListResponse<House>> getHouses(@RequestBody HouseQueryReq hq){
        ListResponse<House> lRes = houseService.getOwnList(hq);
        return RestResponse.success(lRes);
    }

/*    *//**
     * 1.实现分页
     * 2.支持小区搜索、类型搜索
     * 3.支持排序图片、价格、标题、地址等信息
     * 4.支持展示
     * @return
     *//*
    @RequestMapping("/house/list")
    public String houseList(Integer pageSize, Integer pageNum, House query, ModelMap modelMap){
        houseService.queryHouse(query,PageParams.build(pageSize, pageNum));

    }*/



    @RequestMapping("/detail")
    public RestResponse<House> detail(Long houseId){

        //添加房屋热度
        recommendService.increase(houseId);
        //得到房屋详情
        House house = new House();
        house.setId(houseId);

        PageParams pageParams = new PageParams();
        List<House> houseList = houseService.getHouseList(house, pageParams);
        return  houseList.get(0)==null? RestResponse.error(RestCode.HOUSE_NOT_FOUND):
                RestResponse.success(houseList.get(0));


    }

    /**
     * 1.实现分页
     * 2.支持小区搜索、类型搜索
     * 3.支持排序图片、价格、标题、地址等信息
     * 4.支持展示
     * @return
     */
    @RequestMapping("/list")
    public RestResponse<List<House>> list(HouseQueryReq houseQuery){
        House house = new House();
        PageParams pageParams = new PageParams();
        List<House> houseList = houseService.getHouseList(house, pageParams);
        return  houseList==null? RestResponse.error(RestCode.HOUSE_NOT_FOUND):
                RestResponse.success(houseList);
   }


    /*
    推荐房产
     */
    @RequestMapping("recomHouses")
    public RestResponse<List<House>> recomHouses(Integer size){
        List<House> hotHouse = recommendService.getHotHouse(size);
        return RestResponse.success(hotHouse);
 }

     /*
     根据houseId 查询对应房屋代理人userId
      */
     @RequestMapping("queryUserIdByHouseId")
    public RestResponse<Long> selectSaleHouseUser(Long id){
        Long userId = houseService.selectSaleHouseUser(id);
        return RestResponse.success(userId);
    }
     /*
     房屋收藏
      */
    @RequestMapping("/house/bookMark")
    public RestResponse bookMark(HouseUser houseUser){

         houseService.bookMark(houseUser);
         return RestResponse.success();
    }

    /*
     房屋取消收藏
     */
     @RequestMapping("/house/unbookMark")
     public RestResponse unbookMark(HouseUser houseUser){

         houseService.unbookMark(houseUser);
         return RestResponse.success();
     }


}
